﻿using System.Collections.Generic;
using Comsenz.UCenter.Configs;
using Lbb.Common;
using Lbb.Web;

namespace Comsenz.UCenter.Client.API
{
    /// <summary>
    /// 客户端 API 抽象类
    /// </summary>
    /// <author>Chiron</author>
    public abstract class ClientAPIBase : IClientAPI
    {
        #region Fields
        /// <summary>
        /// 客户端API配置
        /// </summary>
        protected readonly static UCenterConfig Config = UCenterConfig.Instance;
        #endregion Fields

        #region Props
        /// <summary>
        /// UCenter 版本标识
        /// </summary>
        public virtual string UC_CLIENT_VERSION
        {
            get
            {
                return "1.6.0";
            }
        }

        public virtual string UC_CLIENT_RELEASE
        {
            get
            {
                return "20110501";
            }
        }
        #endregion Props

        #region Helpers
        protected abstract bool SetLogined(int uid, string username);
        protected abstract bool SetLogoff();

        protected abstract bool DeleteUsers(IEnumerable<int> ids);
        protected abstract bool ChangeUserName(string oldname, string newname);
        #endregion Helpers

        #region IClientAPI 成员

        /// <summary>
        /// 测试通信
        /// </summary>
        public ClientApiReturns Test(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            return ClientApiReturns.Succeed;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        public ClientApiReturns DeleteUser(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            if (!Config.DeleteUser) return ClientApiReturns.Forbidden;

            var ids = reqArgs.Get("ids").Replace("'", string.Empty).ToInts();
            return DeleteUsers(ids) ? ClientApiReturns.Succeed : ClientApiReturns.Failed;
        }

        /// <summary>
        /// 用户改名
        /// </summary>
        public ClientApiReturns RenameUser(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            if (!Config.RenameUser) return ClientApiReturns.Forbidden;

            var oldname = reqArgs.Get("oldusername");
            var newname = reqArgs.Get("newusername");

            if (string.IsNullOrEmpty(oldname) || string.IsNullOrEmpty(newname)) return ClientApiReturns.Failed;
            return ChangeUserName(oldname, newname) ? ClientApiReturns.Succeed : ClientApiReturns.Failed;
        }

        /// <summary>
        /// 同步登录
        /// </summary>
        public ClientApiReturns Synlogin(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            if (!Config.Synlogin) return ClientApiReturns.Forbidden;

            int uid;
            int.TryParse(reqArgs.Get("uid"), out uid);
            var username = reqArgs.Get("username");
            if (string.IsNullOrEmpty(username)) return ClientApiReturns.Failed;

            return SetLogined(uid, username) ? ClientApiReturns.Succeed : ClientApiReturns.Failed;
        }

        /// <summary>
        /// 同步注销
        /// </summary>
        public ClientApiReturns Synlogout(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            if (!Config.Synlogout) return ClientApiReturns.Forbidden;

            SetLogoff();
            return ClientApiReturns.Succeed;
        }

        public ClientApiReturns UpdatePW(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            return Synlogout(reqArgs, reqObj);
        }

        public ClientApiReturns GetTag(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            return ClientApiReturns.Succeed;
        }

        public ClientApiReturns UpdateBadWords(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            return ClientApiReturns.Succeed;
        }

        public ClientApiReturns UpdateHosts(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            return ClientApiReturns.Succeed;
        }

        public ClientApiReturns UpdateApps(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            return ClientApiReturns.Succeed;
        }

        public ClientApiReturns UpdateClient(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            return ClientApiReturns.Succeed;
        }

        public ClientApiReturns UpdateCredit(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            return ClientApiReturns.Succeed;
        }

        public ClientApiReturns GetCreditSettings(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            return ClientApiReturns.Succeed;
        }

        public ClientApiReturns UpdateCreditSettings(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            return ClientApiReturns.Succeed;
        }

        public ClientApiReturns AddFeed(List<QueryParameter> reqArgs, dynamic reqObj)
        {
            return ClientApiReturns.Succeed;
        }

        #endregion
    }
}
